#!/bin/bash

SCRIPTNAME=mount_unionfs
scriptPath=${0%$SCRIPTNAME}
if [ -z "$scriptPath" ]
then
  #invoked from PATH
  scriptPath=$(which $0)
  scriptPath=${scriptPath%$SCRIPTNAME}
fi
source ${scriptPath}vbs_common_functions

DAT_UID=$(id -u $USER)
DAT_GID=$(getent group $GROUP | cut -d: -f3)

DISKS=$(cat $CFGFILE |grep n_drives |cut -d ' ' -f 3)
DISKS=${DISKS%?}

which unionfs-fuse
if [ "$?" == "0" ]
then
  UNIONPOINT=/mnt/unionfs
  if [ ! -d "$UNIONPOINT" ]
  then
    echo "Creating mountpoint $UNIONPOINT"
    mkdir -p $UNIONPOINT
  fi
  echo "Mounting unionfs-fuse to $UNIONPOINT"
  DISKS=$((DISKS-1))
  unionfs-fuse $(for l in $(seq 0 $DISKS) ; do printf "%s%d" "${ROOTDIRS}" $l ; if [ "$DISKS" != "$l" ] ; then printf "%s" ":" ; fi; done) -o allow_other $UNIONPOINT
else
  echo "No unionfs-fuse found on your system!"
  echo "TODO: binary could vary between systems"
fi
